// StringTree.cpp: implementation of the CStringTree class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include ".h"
#include "StringTree.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_SERIAL (CStringTree, CObject, 1)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CStringTree::CStringTree(CString name)
{
	m_sName = name;
}

CStringTree::CStringTree()
{

}

CStringTree::~CStringTree()
{
	Destroy();
}

CStringTree* CStringTree::Add(CString name, CStringTree* parent)
{
	CStringTree * lt = new CStringTree(name);
	if (name=="10520 - \"FDGO - vspro.tar.gz\" - TfrmGO")
		int i=0;
	m_oTree.Add(lt);
	if (parent==NULL)
		m_tParent=this;
	else
		m_tParent=parent;
	return lt;
}

void CStringTree::Destroy()
{
	int i;
	for (i=0; i<m_oTree.GetSize(); i++)
	{
		CStringTree *t = (CStringTree*)m_oTree.GetAt(i);
		t->Destroy();
		m_sName.Delete(0,m_sName.GetLength());
		
		//t->~CStringTree();
		delete t;
	}
	//OutputDebugStringA("m_oTree.RemoveAll\r\n");
	m_oTree.RemoveAll();
	//OutputDebugStringA("CStringTree Finished\r\n");
}

CStringTree* CStringTree::GetChildAt(int nIndex)
{
	if (m_oTree.GetSize()-1 <nIndex)
		return NULL;
	else
		return (CStringTree*)m_oTree.GetAt(nIndex);
}

CString CStringTree::GetName()
{
	return m_sName;
}

CString CStringTree::GetString(int indentlevel)
{
	CString b = "";
	int i;
	for (i=0; i<indentlevel; i++)
		b +="  ";
	
	b += m_sName + "\r\n";

	for (i=0; i<m_oTree.GetSize(); i++)
	{
		b+=((CStringTree*)m_oTree.GetAt(i))->GetString(indentlevel+1);
	}
	return b;
}

CStringTree* CStringTree::GetParent()
{
	return m_tParent;
}

void CStringTree::Serialize(CArchive &ar)
{
	CObject::Serialize(ar);
	if (ar.IsStoring()) 
	{
		ar << m_sName << m_sToolTip << m_rcLocation;
		m_oTree.Serialize(ar);
	}
	else
	{
		ar >> m_sName >> m_sToolTip >> m_rcLocation;
		m_oTree.Serialize(ar);
		for(int i = 0; i<m_oTree.GetSize(); i++)
			((CStringTree*)m_oTree.GetAt(i))->m_tParent = this;
	}
}

void CStringTree::TreeExport(CTreeCtrl *tree, HTREEITEM* parent, BOOL bSomething, CArray<HTREEITEM,HTREEITEM> & treeItems, CStringArray & strings, CArray<CRect,CRect> & rects)
{
	HTREEITEM h;
	if (parent == NULL) // This is tree trunk (Desktop Window)
		h=tree->InsertItem(m_sName,0,0);
	else
		h=tree->InsertItem(m_sName,0,0,*parent);
	treeItems.Add(h);
	CString szTemp;
	szTemp.Format("Location: {%d,%d,%d,%d}",m_rcLocation);
	if (m_sToolTip.GetLength()>0)
		m_sToolTip+=szTemp;
	strings.Add(m_sToolTip);
	rects.Add(m_rcLocation);
	int i;
	for (i=0; i<m_oTree.GetSize(); i++)
		((CStringTree*)m_oTree.GetAt(i))->TreeExport(tree,&h,bSomething,treeItems,strings,rects);
}
